package cisdi.mes.sm.rm.serviceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cisdi.mes.sm.rm.dao.MeasureDocDao;
import cisdi.mes.sm.rm.dao.MeasureDocLogDao;
import cisdi.mes.sm.rm.dao.QualDetDao;
import cisdi.mes.sm.rm.dao.StorageContentDao;
import cisdi.mes.sm.rm.dao.StorageDao;
import cisdi.mes.sm.rm.domain.SmRmMeasureDoc;
import cisdi.mes.sm.rm.domain.SmRmQualityElement;
import cisdi.mes.sm.rm.domain.SmRmStorage;
import cisdi.mes.sm.rm.service.MatUnloadMgr;
import cisdi.mes.sm.util.dao.BaseDao;
import cisdi.mes.sm.util.dao.IPcsDao;

@Service
public class MatUnloadMgrImpl implements MatUnloadMgr {

	@Autowired
	protected MeasureDocDao measureDocDao;
	@Autowired
	private StorageContentDao storageContentDao;
	@Autowired
	private MeasureDocLogDao measureDocLogDao;
	@Autowired
	protected BaseDao baseDao;
	@Autowired
	private QualDetDao qualDetDao;
	@Autowired
	private StorageDao storageDao;
	@Autowired
	private IPcsDao pcsDao;
	@Autowired
	public IPcsDao getPcsDao() {
		return pcsDao;
	}

	public void setPcsDao(IPcsDao pcsDao) {
		this.pcsDao = pcsDao;
	}

	public StorageDao getStorageDao() {
		return storageDao;
	}

	public void setStorageDao(StorageDao storageDao) {
		this.storageDao = storageDao;
	}

	public QualDetDao getQualDetDao() {
		return qualDetDao;
	}

	public void setQualDetDao(QualDetDao qualDetDao) {
		this.qualDetDao = qualDetDao;
	}

	public MeasureDocDao getMeasureDocDao() {
		return measureDocDao;
	}

	public void setMeasureDocDao(MeasureDocDao measureDocDao) {
		this.measureDocDao = measureDocDao;
	}

	public BaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	public StorageContentDao getStorageContentDao() {
		return storageContentDao;
	}

	public void setStorageContentDao(StorageContentDao storageContentDao) {
		this.storageContentDao = storageContentDao;
	}

	public MeasureDocLogDao getMeasureDocLogDao() {
		return measureDocLogDao;
	}

	public void setMeasureDocLogDao(MeasureDocLogDao measureDocLogDao) {
		this.measureDocLogDao = measureDocLogDao;
	}

	@Transactional
	@Override
	public Map<String, Object> matUnload(String measureId, String to, Double weight,Double totalWgt,HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();

		SmRmMeasureDoc md = measureDocDao.getById(measureId);
		String validString = validMatUnload(md, to, weight, totalWgt);

		if (validString.equals("success")) {
			
			// 1更新SmRmMeasureDoc中重量
			measureDocDao.matUnload(md, weight);
			// 2增加目的料仓物料重量,SmRmStorageContent
			storageContentDao.store(md, to, weight);
			// 3追踪计量单LOG
			measureDocLogDao.logMatUnload(md, to, weight,  request);
			// 4发高位料仓物料和成分
			SmRmStorage storage = storageDao.getById(to);
			if (storage.getStorageType().equals("B")) {
				String matId = md.getMaterialId();
				String qualBat = md.getQualityBatch();
				String agg = storage.getStorageLoc();
				pcsDao.sendBin(storage, matId, qualBat);
				pcsDao.sendAna(agg, qualBat, matId);
			}
			map.put("success", true);
			map.put("message", "计量单移至料仓成功");
		} else {
			map.put("success", false);
			map.put("message", validString);
		}

		return map;
	}


	private String validMatUnload(SmRmMeasureDoc md, String to, Double weight, Double totalWgt) {
		String validString = "";

		// 1.查看重量是否足够
		Boolean isWgtEnough = false;
		Double restWgt = md.getSurplusNetWeight();
		
		if (restWgt >= weight) {
			isWgtEnough = true;
		} else {
			validString += "入库重量>计量单的剩余重量<br>";
		}

		Boolean isMeasureAna = false;
		String qualBat = md.getQualityBatch();
		List<SmRmQualityElement> anas = qualDetDao.getAna(qualBat);
		if (anas != null && anas.size() > 0) {
			isMeasureAna = true;
		} else {
			validString += "该计量单关联的质检批未做成分判定<br>";
		}

		Double volume = storageDao.getById(to).getVolume();
		boolean isVolume = true;
		if((totalWgt+weight) > volume ){
			isVolume = false;
			validString += "下料后超过该料仓容量，请重新输入重量！";
		}
		if (isWgtEnough && isMeasureAna && isVolume) {
			validString = "success";
		}

		return validString;
	}

}
